package org.glut.service;

import org.glut.bean.YHAir;
import org.glut.serialport.manager.SerialPortManager;
import org.glut.serialport.ui.MainFrame;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Scope("singleton")
public class YHAirService {
    @Autowired
    MessageService messageService;
    //    @Autowired
//    private  static MainFrame mainFrame;
    @Autowired
    public static YHAir yhAir1;
    public static YHAir yhAir2;
    public List<String> yhtimeList1 = new ArrayList<>();
    public List<String> yhnumList1 = new ArrayList<>();
    public List<String> yhpower1 = new ArrayList<>();
    public List<String> yhtimeList2 = new ArrayList<>();
    public List<String> yhnumList2 = new ArrayList<>();
    public List<String> yhpower2 = new ArrayList<>();
    private SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss");//设置时间

    public YHAirService() {
        yhAir1 = new YHAir();
        yhAir2 = new YHAir();
//        (CFAir)GetApplicationContext.getBean("CFAir")
    }


    public void getmsgs(String[] message) {
//        for (String s : message) {
//            System.out.println(s);
//        }
//        yhAir = (CFAir) GetApplicationContext.getBean("CFAir");
        char id = message[0].charAt(2);
        if (id == '1') {
            initYH(yhAir1,message,yhtimeList1,yhnumList1,yhpower1);
        } else if (id == '2') {
            initYH(yhAir2,message,yhtimeList2,yhnumList2,yhpower2);
        }


//            System.out.println(timeList);
//            System.out.println(yhList);
    }

    private void initYH(YHAir yhAir, String[] message, List<String> yhtimeList, List<String> yhnumList, List<String> yhpower) {
        yhAir.setId(message[0].charAt(2)+"");
        yhAir.setPower(message[1].substring(1));
        yhAir.setCurNum(message[2].substring(1));
        yhAir.setMax(message[3].substring(1));
        yhAir.setStatus(message[4].substring(1));
        yhAir.setNumWarm(message[5].substring(1));
        yhAir.setPowerWarm(message[6].substring(1));
        String time = df.format(new Date());
        yhtimeList.add(time);
        yhnumList.add(yhAir.getCurNum());
        yhpower.add(yhAir.getPower());
    }

    /**
     * 发送命令控制有害气体
     * @param num
     * @param auto
     * @param status
     */
    public void setCmd(String num, String auto, String status,String id) {
        String cmd = "YH"+id+"A1M" + auto + "T" + num + "V" + status;

        System.out.println(cmd);
        if (messageService.outputStream != null) {
            try {
                messageService.outputStream.write(cmd.getBytes());
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else {
            System.out.println(cmd);
//            System.out.println("------socket未连接，outputStream为空------");
        }
//        mainFrame = (MainFrame) GetApplicationContext.getBean("MainFrame");

        try {
            if (MainFrame.mSerialport != null) {
                SerialPortManager.sendToPort(MainFrame.mSerialport, cmd.getBytes());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
